package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.vo.CheckItemVo;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    /**
     * 添加检查项
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItem.setCreatTime(LocalDateTime.now());
        checkItem.setUpdateTime(LocalDateTime.now());
        checkItemMapper.add(checkItem);
    }

    /**
     * 删除检查项
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        checkItemMapper.deleteById(id);
    }

    /**
     * 修改检查项
     * @param checkItem
     */
    @Override
    public void update(CheckItem checkItem) {
        checkItem.setUpdateTime(LocalDateTime.now());

        checkItemMapper.update(checkItem);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @Override
    public List<CheckItemVo> findAll() {
       List<CheckItem> checkItems =checkItemMapper.findAll();
       List<CheckItemVo> checkItemVoList = new ArrayList<>();

        for (CheckItem checkItem : checkItems) {
            CheckItemVo checkItemVo = new CheckItemVo();
            BeanUtils.copyProperties(checkItem, checkItemVo);
            checkItemVoList.add(checkItemVo);
        }
        return checkItemVoList;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckItemVo> page = checkItemMapper.findPage(queryPageBean);



        return new PageResult(page.getTotal(),page.getResult());

    }
}
